library(tidyverse)

# Set the directory to the location of the replication data
setwd("")

# VIGNETTE DATA
US_Citizen_Vignette <- read_csv("Data_Cleaned/US_Citizen_Vignette.csv")
US_Politician_Vignette <- read_csv("Data_Cleaned/US_Politician_Vignette.csv")
CL_Citizen_Vignette <- read_csv("Data_Cleaned/CL_Citizen_Vignette.csv")
CL_Politician_Vignette <- read_csv("Data_Cleaned/CL_Politician_Vignette.csv")
DK_Citizen_Vignette <- read_csv("Data_Cleaned/DK_Citizen_Vignette.csv")
DK_Politician_Vignette <- read_csv("Data_Cleaned/DK_Politician_Vignette.csv")
BE_Citizen_Vignette <- read_csv("Data_Cleaned/BE_Citizen_Vignette.csv")
BE_Politician_Vignette <- read_csv("Data_Cleaned/BE_Politician_Vignette.csv")

# Combined
V <- bind_rows(US_Citizen_Vignette, US_Politician_Vignette,
               CL_Citizen_Vignette, CL_Politician_Vignette,
               DK_Citizen_Vignette, DK_Politician_Vignette,
               BE_Citizen_Vignette, BE_Politician_Vignette)

# Order factor levels
V <- V %>%
     mutate(country = factor(country),
            resp_gender = factor(resp_gender, levels = c("Male", "Female")),
            resp_ideology_discrete = factor(resp_ideology_discrete, levels = c("Right-wing", "Center", "Left-wing")),
            resp_exposure_binary = factor(resp_exposure_binary, levels = c("Has not experienced social media harassment", "Experienced social media harassment")),
            woman_pol = factor(woman_pol, levels = c("Man politician", "Woman politician")),
            woman_user = factor(woman_user, levels = c("Woman user", "Man user")),
            text_pol = as.factor(text_pol),
            text_pol_group = factor(text_pol_group, levels = c("Healthcare", "Education", "Economy", "Crime", "National security")),
            text_user = as.factor(text_user),
            gendered = factor(gendered, levels = c("Non-gendered text", "Gendered text")),
            party_pol_copartisan = factor(party_pol_copartisan, levels = c("Non co-partisan", "Co-partisan")),
            party_pol = factor(party_pol, levels = c("Socialdemokratiet",
                                                     "Venstre",
                                                     "Socialistisk Folkeparti",
                                                     "Nye Borgerlige",
                                                     "Renovación Nacional",
                                                     "Partido Socialista de Chile",
                                                     "Revolución Democrática",
                                                     "Vooruit",
                                                     "CD&V",
                                                     "N-VA",
                                                     "Democratic Party",
                                                     "Republican Party")),
            task_number = factor(task_number))

# Write out vignette data without identifying information
write_rds(V, "Data/Vignette_Data.rds")


# CONJOINT DATA

US_Citizen_Conjoint <- read_csv("Data_Cleaned/US_Citizen_Conjoint.csv")
US_Politician_Conjoint <- read_csv("Data_Cleaned/US_Politician_Conjoint.csv")
CL_Citizen_Conjoint <- read_csv("Data_Cleaned/CL_Citizen_Conjoint.csv")
CL_Politician_Conjoint <- read_csv("Data_Cleaned/CL_Politician_Conjoint.csv")
DK_Citizen_Conjoint <- read_csv("Data_Cleaned/DK_Citizen_Conjoint.csv")
DK_Politician_Conjoint <- read_csv("Data_Cleaned/DK_Politician_Conjoint.csv")
BE_Citizen_Conjoint <- read_csv("Data_Cleaned/BE_Citizen_Conjoint.csv")
BE_Politician_Conjoint <- read_csv("Data_Cleaned/BE_Politician_Conjoint.csv")

# Combined data
C <- bind_rows(US_Citizen_Conjoint, US_Politician_Conjoint,
               CL_Citizen_Conjoint, CL_Politician_Conjoint,
               DK_Citizen_Conjoint, DK_Politician_Conjoint,
               BE_Citizen_Conjoint, BE_Politician_Conjoint)

# Order factor levels
C <- C %>%
     mutate(country = factor(country),
            resp_gender = factor(resp_gender, levels = c("Male", "Female")),
            resp_ideology_discrete = factor(resp_ideology_discrete, levels = c("Right-wing", "Center", "Left-wing")),
            resp_exposure_binary = factor(resp_exposure_binary, levels = c("Has not experienced social media harassment", "Experienced social media harassment")),
            poc_pol = factor(poc_pol, levels = c("White politician", "Person of Color politician")),
            woman_pol = factor(woman_pol, levels = c("Man politician", "Woman politician")),
            woman_user = factor(woman_user, levels = c("Woman user", "Man user")),
            text_pol = as.factor(text_pol),
            text_pol_group = factor(text_pol_group, levels = c("National security", "Healthcare", "Education", "Economy", "Crime")),
            text_user = as.factor(text_user),
            gendered = factor(gendered, levels = c("Non-gendered text", "Gendered text")),
            party_pol_copartisan = factor(party_pol_copartisan, levels = c("Non co-partisan", "Co-partisan")),
            party_pol = factor(party_pol, levels = c("Socialdemokratiet",
                                                     "Venstre",
                                                     "Socialistisk Folkeparti",
                                                     "Nye Borgerlige",
                                                     "Renovación Nacional",
                                                     "Partido Socialista de Chile",
                                                     "Revolución Democrática",
                                                     "Vooruit",
                                                     "CD&V",
                                                     "N-VA",
                                                     "Democratic Party",
                                                     "Republican Party")),
            task_number = factor(task_number, levels = c("Task 1",
                                                         "Task 2",
                                                         "Task 3",
                                                         "Task 4",
                                                         "Task 5")))

C$woman_pol_respondent_type <- interaction(C$woman_pol, C$respondent_type, sep = " & ")
C$woman_pol_respondent_type <- factor(C$woman_pol_respondent_type,
                               levels = c("Man politician & Citizen",
                                          "Woman politician & Politician",
                                          "Woman politician & Citizen",
                                          "Man politician & Politician"))

C$woman_pol_gendered <- interaction(C$woman_pol, C$gendered, sep = " & ")
C$woman_pol_gendered <- factor(C$woman_pol_gendered,
                               levels = c("Man politician & Gendered text",
                                          "Woman politician & Gendered text",
                                          "Woman politician & Non-gendered text",
                                          "Man politician & Non-gendered text"))

C$woman_pol_man_user <- interaction(C$woman_pol, C$woman_user, sep = " & ")
C$woman_pol_man_user <- factor(C$woman_pol_man_user,
                               levels = c("Man politician & Man user",
                                          "Woman politician & Man user",
                                          "Woman politician & Woman user",
                                          "Man politician & Woman user"))

C$woman_pol_resp_woman <- interaction(C$woman_pol, C$resp_gender, sep = " & ")
C$woman_pol_resp_woman <- factor(C$woman_pol_resp_woman,
                               levels = c("Man politician & Female",
                                          "Woman politician & Female",
                                          "Woman politician & Male",
                                          "Man politician & Male"))

C$gendered_resp_woman <- interaction(C$gendered, C$resp_gender, sep = " & ")
C$gendered_resp_woman <- factor(C$gendered_resp_woman,
                               levels = c("Gendered text & Male",
                                          "Gendered text & Female",
                                          "Non-gendered text & Male",
                                          "Non-gendered text & Female"))

C$resp_left_wing <- ifelse(C$resp_ideology_discrete == "Left-wing", "Left-wing", "Center/Right-wing")
C$resp_left_wing <- factor(C$resp_left_wing, levels = c("Center/Right-wing", "Left-wing"))

C$woman_pol_resp_left_wing <- interaction(C$woman_pol, C$resp_left_wing, sep = " & ")
C$woman_pol_resp_left_wing <- factor(C$woman_pol_resp_left_wing,
                               levels = c("Man politician & Left-wing",
                                          "Woman politician & Left-wing",
                                          "Woman politician & Center/Right-wing",
                                          "Man politician & Center/Right-wing"))

C$man_user_resp_left_wing <- interaction(C$woman_user, C$resp_left_wing, sep = " & ")
C$man_user_resp_left_wing <- factor(C$man_user_resp_left_wing,
                               levels = c("Woman user & Left-wing",
                                          "Man user & Left-wing",
                                          "Woman user & Center/Right-wing",
                                          "Man user & Center/Right-wing"))

C$man_user_gendered <- interaction(C$woman_user, C$gendered, sep = " & ")
C$man_user_gendered <- factor(C$man_user_gendered,
                               levels = c("Woman user & Gendered text",
                                          "Man user & Gendered text",
                                          "Woman user & Non-gendered text",
                                          "Man user & Non-gendered text"))

C$man_user_resp_woman <- interaction(C$woman_user, C$resp_gender, sep = " & ")
C$man_user_resp_woman <- factor(C$man_user_resp_woman,
                               levels = c("Woman user & Female",
                                          "Man user & Male",
                                          "Woman user & Male",
                                          "Man user & Female"))

C$woman_pol_man_user_resp_left_wing <- interaction(C$woman_pol, C$woman_user, C$resp_left_wing, sep = " & ")
C$woman_pol_man_user_resp_left_wing <- factor(C$woman_pol_man_user_resp_left_wing,
                                              levels = c("Man politician & Man user & Left-wing",
                                                         "Woman politician & Man user & Left-wing",
                                                         "Woman politician & Woman user & Left-wing",
                                                         "Man politician & Woman user & Left-wing",
                                                         "Man politician & Man user & Center/Right-wing",
                                                         "Woman politician & Man user & Center/Right-wing",
                                                         "Woman politician & Woman user & Center/Right-wing",
                                                         "Man politician & Woman user & Center/Right-wing"))

C$woman_pol_man_user_resp_woman <- interaction(C$woman_pol, C$woman_user, C$resp_gender, sep = " & ")
C$woman_pol_man_user_resp_woman <- factor(C$woman_pol_man_user_resp_woman,
                                          levels = c("Man politician & Man user & Female",
                                                     "Woman politician & Man user & Female",
                                                     "Woman politician & Woman user & Female",
                                                     "Man politician & Woman user & Female",
                                                     "Man politician & Man user & Male",
                                                     "Woman politician & Man user & Male",
                                                     "Woman politician & Woman user & Male",
                                                     "Man politician & Woman user & Male"))

C$woman_pol_man_user_gendered <- interaction(C$woman_pol, C$woman_user, C$gendered, sep = " & ")
C$woman_pol_man_user_gendered <- factor(C$woman_pol_man_user_gendered,
                                        levels = c("Man politician & Man user & Gendered text",
                                                   "Woman politician & Man user & Gendered text",
                                                   "Woman politician & Woman user & Gendered text",
                                                   "Man politician & Woman user & Gendered text",
                                                   "Man politician & Man user & Non-gendered text",
                                                   "Woman politician & Man user & Non-gendered text",
                                                   "Woman politician & Woman user & Non-gendered text",
                                                   "Man politician & Woman user & Non-gendered text"))

C$woman_pol_gendered_resp_woman <- interaction(C$woman_pol, C$gendered, C$resp_gender, sep = " & ")
C$woman_pol_gendered_resp_woman <- factor(C$woman_pol_gendered_resp_woman,
                                          levels = c("Woman politician & Non-gendered text & Female",
                                                     "Woman politician & Gendered text & Female",
                                                     "Man politician & Gendered text & Female",
                                                     "Man politician & Non-gendered text & Female",
                                                     "Woman politician & Non-gendered text & Male",
                                                     "Woman politician & Gendered text & Male",
                                                     "Man politician & Gendered text & Male",
                                                     "Man politician & Non-gendered text & Male"))

C$woman_pol_task_number <- interaction(C$woman_pol, C$task_number, sep = " x ")
C$woman_pol_task_number <- factor(C$woman_pol_task_number,
                                  levels = c("Woman politician x Task 1",
                                             "Woman politician x Task 2",
                                             "Woman politician x Task 3",
                                             "Woman politician x Task 4",
                                             "Woman politician x Task 5",
                                             "Man politician x Task 1",
                                             "Man politician x Task 2",
                                             "Man politician x Task 3",
                                             "Man politician x Task 4",
                                             "Man politician x Task 5"))

C$woman_pol_text_pol_group <- interaction(C$woman_pol, C$text_pol_group, sep = " x ")
C$woman_pol_text_pol_group <- factor(C$woman_pol_text_pol_group,
                                  levels = c("Woman politician x Economy",
                                             "Woman politician x Education",
                                             "Woman politician x Healthcare",
                                             "Woman politician x National security",
                                             "Woman politician x Crime",
                                             "Man politician x Economy",
                                             "Man politician x Education",
                                             "Man politician x Healthcare",
                                             "Man politician x National security",
                                             "Man politician x Crime"))

C$woman_poc_pol <- interaction(C$woman_pol, C$poc_pol, sep = " x ")
C$woman_poc_pol <- factor(C$woman_poc_pol,
                          levels = c("Man politician x White politician",
                                     "Woman politician x Person of Color politician",
                                     "Woman politician x White politician",
                                     "Man politician x Person of Color politician"))

# Write out paired conjoint data
write_rds(C, "Data/Paired_Conjoint_Data.rds")



